package com.venus.lms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.lms.pojo.Book;
import com.venus.lms.pojo.BookWithUser;
import com.venus.lms.service.BookService;
import com.venus.lms.service.BookWithUserService;
import com.venus.lms.utils.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图书控制层
 *
 * @author DQM
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    BookService bookService;

    @Resource
    BookWithUserService bookWithUserService;

    /**
     * 图书上架
     */
    @PostMapping
    public Result<?> save(@RequestBody Book book) {
        Book selectOne = bookService.getBookByIsbn(book.getIsbn());
        if (selectOne != null) {
            return Result.error("-1", "图书编号已存在!");
        }
        bookService.save(book);
        return Result.success();
    }

    /**
     * 图书修改
     */
    @PutMapping
    public Result<?> update(@RequestBody Book book) {
        Book selectOne = bookService.getBookByIsbn(book.getIsbn(), book.getId());
        if (selectOne != null) {
            return Result.error("-1", "修改后的图书编号已存在!");
        }
        bookService.updateById(book);
        return Result.success();
    }

    /**
     * 图书批量下架
     */
    @PostMapping("/deleteBatch")
    @Transactional
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            Book book = bookService.getBookById(id);
            BookWithUser bookWithUser = bookWithUserService.getBookWithUserByIsbn(book.getIsbn());
            if (bookWithUser != null) {
                return Result.error("-1", "书籍在借阅中,无法下架");
            }
        }
        bookService.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 图书下架
     */
    @DeleteMapping("/{id}")
    @Transactional
    public Result<?> delete(@PathVariable Long id) {
        Book book = bookService.getBookById(id.intValue());
        BookWithUser bookWithUser = bookWithUserService.getBookWithUserByIsbn(book.getIsbn());
        if (bookWithUser != null) {
            return Result.error("-1", "书籍在借阅中,无法下架");
        }
        bookService.deleteById(id);
        return Result.success();
    }

    /**
     * 分页展示图书信息
     *
     * @param pageNum  当前页
     * @param pageSize 一页几条数据
     * @param search1  图书编号
     * @param search2  书名
     * @param search3  作者
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        LambdaQueryWrapper<Book> wrappers = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(Book::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(Book::getName, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(Book::getAuthor, search3);
        }
        wrappers.orderByDesc(Book::getBorrownum);    //按借阅次数降序排序
        Page<Book> BookPage = bookService.selectPage(new Page<>(pageNum, pageSize), wrappers);
        return Result.success(BookPage);
    }
}
